package com.yyy.util;

import javax.swing.*;
import java.awt.*;
import java.io.File;

/**
 * @Author：yyy
 * @Package：com.yyy.util
 * @Project：HuTuBiill
 * @name：GUIUtil
 * @Date：2024/11/7 22:17
 * @Filename：GUIUtil
 */
public class GUIUtil {
    public static String ImageFolder="E:\\workspace\\JAVA\\project_practiace\\HuTuZhang\\HuTuBiill\\src\\image";

    //给按钮设置背景图片及标题
    public static void setImageIcon(JButton b,String fileName,String tip){
        ImageIcon imageIcon=new ImageIcon(new File(ImageFolder,fileName).getAbsolutePath());
        b.setIcon(imageIcon);
        b.setPreferredSize(new Dimension(61,81));
        b.setToolTipText(tip);
        b.setVerticalTextPosition(JButton.BOTTOM);
        b.setHorizontalTextPosition(JButton.CENTER);
        b.setText(tip);
    }


    //给组件设置颜色
    public static void setColor(Color color,JComponent... cs){
        for (JComponent c:cs){
            c.setForeground(color);
        }
    }
    //配置皮肤
    public static void useLNF(){
        try{
            javax.swing.UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //展示面板
    public static void showPanel(JPanel p,double strechRate){
        JFrame frame=new JFrame();
        frame.setSize(500,500);
        frame.setLocationRelativeTo(null);
        CenterPanel cp=new CenterPanel(strechRate);
        frame.setContentPane(cp);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        cp.show(p);
    }


    /*
    * 判断输入框内容是否为空
    * */
    public static boolean checkEmpty(JTextField tf,String input){
        String text=tf.getText().trim();
        if(text.length()==0){
            JOptionPane.showMessageDialog(null,input+"不能为空");
            tf.grabFocus();//聚焦
            return false;
        }
        return true;
    }




    public static void showPannel(JPanel p){
        showPanel(p,0.85);
    }

    /*
    *
    * 校验一个组件内容是否是数字格式
    * */
    public static boolean checkNumber(JTextField tf,String input){
        if(!checkEmpty(tf,input))
            return false;
        String text=tf.getText().trim();

        try {
            Integer.parseInt(text);
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null,input+" 需要是整数");
            tf.grabFocus();//聚焦
            return false;
        }
        return true;
    }

    /*
    *
    * 校验一个组件的内容是否是零
    * */
    public static boolean checkZero(JTextField tf,String input){
        if(!checkNumber(tf,input))
            return false;
        String text=tf.getText().trim();
        if(Integer.parseInt(text)==0){
            JOptionPane.showMessageDialog(null,input+" 不能为零");
            tf.grabFocus();//聚焦
            return false;
        }
        return true;
    }






































}
